﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace CNdotnet.Ezsocio.ContactService.Email {
   public  class EmailSohuVipService :IEmailService{
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
       public string EmailSystem {
           get { return "vip.sohu.com/"; }
       }

       public string StartUrl {
           get { return "http://vip.sohu.com/"; }
       }

       public Encoding DefaultEncoding {
           get { return Encoding.GetEncoding("gb2312"); }
       }

        #endregion

        #region Events

        #endregion
     
       public MailContractCollection GetContracts(string userName, string pwd) {
           MailContractCollection mc = new MailContractCollection();
           string returnUrl = StartUrl;
           CookieContainer cc = new CookieContainer();         
           string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
           string language = "zh-cn";
           string refer = "";
           string contentType = "application/x-www-form-urlencoded";
           string htm = WebOpHelper.GetHtml(StartUrl, accept, contentType, refer,
               language, cc, Encoding.GetEncoding("gb2312"), out returnUrl);
           var sgs = WebOpHelper.ResoveList(htm, "document.form_login.sg.value=\"", "\"");
           var rus = WebOpHelper.ResoveList(htm, "document.form_login.ru.value=\"", "\"");
           var eru = WebOpHelper.Resove(htm, "document.form_login.eru.value = \"", "\"+shortname") + userName;
           var vr = WebOpHelper.Resove(htm, "<INPUT type=\"hidden\"    id=\"vr\"  name=\"vr\"     value=\"", "\"");
           var ct = WebOpHelper.Resove(htm, "<INPUT type=\"hidden\"    id=\"ct\"  name=\"ct\"     value=\"", "\"");
           var fl = WebOpHelper.Resove(htm, "<INPUT type=\"hidden\"    id=\"fl\"  name=\"fl\"     value=\"", "\"");
           var loginid = userName + "@vip.sohu.com";
           var appID = WebOpHelper.Resove(htm, "<INPUT type=\"hidden\"    id=\"appid\"   name=\"appid\"     value=\"", "\"");
           var nvc = new NameValueCollection();
           nvc.Add("username", userName);
           nvc.Add("passwd", pwd);
           nvc.Add("loginid", loginid);
           nvc.Add("appid", appID);
           nvc.Add("ru", rus[1]);
           nvc.Add("fl", fl);
           nvc.Add("ct", ct);
           nvc.Add("vr", vr);
           nvc.Add("sg", sgs[1]);
           nvc.Add("eru", eru);  
           string postContent = WebOpHelper.GetNameValueRowData(nvc,'&');
           refer = StartUrl;
           htm = WebOpHelper.Post("http://passport.sohu.com/login.jsp", accept, contentType, refer, language, postContent, cc, DefaultEncoding, out returnUrl);
           Uri u = new Uri(returnUrl);
           string addressUrl = WebOpHelper.ResoveReverse(htm, "\" class=a01>地 址 簿", "<a href=\"");
           addressUrl = string.Concat(u.Scheme+"://" + u.Host, addressUrl);
           htm = WebOpHelper.GetHtml(addressUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
           List<string> list = WebOpHelper.ResoveList(htm, "title=\"发送电子邮件\">", "</a>");
           foreach (string s in list) {
               MailContract m = new MailContract();
               m.Email = s;
               mc.Add(m);
           }
           List<string> list2 = WebOpHelper.ResoveList(htm,"title=\"查看/编辑\">","</a>");
           int i = 0;
           foreach (string s in list2) {
               if(string.IsNullOrEmpty(s))
               {
                   mc[i].Name = mc[i].Email;
               }
               else{
                   mc[i].Name = s;
               }
               i++;
           }
           return mc;
       }
   }
}
